#include "progressdialog.h"
#include <QApplication>
#include <QScreen>

ProgressDialog::ProgressDialog(const QString& title, QWidget* parent)
    : QDialog(parent)
    , m_titleLabel(new QLabel(title, this))
    , m_detailLabel(new QLabel(this))
    , m_progressBar(new QProgressBar(this))
    , m_cancelButton(new QPushButton(tr("取消"), this))
    , m_animationTimer(new QTimer(this))
    , m_currentValue(0)
    , m_isAnimating(false)
{
    setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    
    initUI();
    setupConnections();
    setupStyles();
    
    // 设置窗口大小和位置
    resize(400, 200);
    QScreen* screen = QApplication::primaryScreen();
    QRect screenGeometry = screen->geometry();
    move((screenGeometry.width() - width()) / 2,
         (screenGeometry.height() - height()) / 2);
}

ProgressDialog::~ProgressDialog()
{
}

void ProgressDialog::setValue(int value)
{
    if (value < 0) value = 0;
    if (value > 100) value = 100;
    
    m_currentValue = value;
    m_progressBar->setValue(value);
    
    // 更新详细信息
    QString detailText = tr("正在处理... %1%").arg(value);
    setDetailText(detailText);
}

void ProgressDialog::setDetailText(const QString& text)
{
    m_detailLabel->setText(text);
}

void ProgressDialog::setShowCancelButton(bool show)
{
    m_cancelButton->setVisible(show);
}

void ProgressDialog::setProgressStyle(const QString& style)
{
    m_progressBar->setStyleSheet(style);
}

void ProgressDialog::setWindowStyle(const QString& style)
{
    setStyleSheet(style);
}

void ProgressDialog::onCancelClicked()
{
    emit canceled();
    close();
}

void ProgressDialog::updateProgressAnimation()
{
    if (!m_isAnimating) return;
    
    int currentValue = m_progressBar->value();
    if (currentValue < m_currentValue) {
        m_progressBar->setValue(currentValue + 1);
    } else {
        m_animationTimer->stop();
        m_isAnimating = false;
    }
}

void ProgressDialog::initUI()
{
    // 创建主布局
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(15);
    mainLayout->setContentsMargins(20, 20, 20, 20);
    
    // 设置标题标签
    m_titleLabel->setAlignment(Qt::AlignCenter);
    QFont titleFont = m_titleLabel->font();
    titleFont.setPointSize(14);
    titleFont.setBold(true);
    m_titleLabel->setFont(titleFont);
    
    // 设置进度条
    m_progressBar->setMinimum(0);
    m_progressBar->setMaximum(100);
    m_progressBar->setValue(0);
    m_progressBar->setTextVisible(true);
    m_progressBar->setFormat("%p%");
    
    // 设置详细信息标签
    m_detailLabel->setAlignment(Qt::AlignCenter);
    m_detailLabel->setWordWrap(true);
    
    // 设置取消按钮
    m_cancelButton->setFixedWidth(80);
    
    // 创建按钮布局
    QHBoxLayout* buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch();
    buttonLayout->addWidget(m_cancelButton);
    buttonLayout->addStretch();
    
    // 添加所有组件到主布局
    mainLayout->addWidget(m_titleLabel);
    mainLayout->addWidget(m_progressBar);
    mainLayout->addWidget(m_detailLabel);
    mainLayout->addLayout(buttonLayout);
}

void ProgressDialog::setupConnections()
{
    connect(m_cancelButton, &QPushButton::clicked, this, &ProgressDialog::onCancelClicked);
    connect(m_animationTimer, &QTimer::timeout, this, &ProgressDialog::updateProgressAnimation);
}

void ProgressDialog::setupStyles()
{
    // 设置窗口样式
    QString windowStyle = R"(
        QDialog {
            background-color: #ffffff;
            border: 1px solid #cccccc;
            border-radius: 8px;
        }
    )";
    setWindowStyle(windowStyle);
    
    // 设置进度条样式
    QString progressStyle = R"(
        QProgressBar {
            border: 1px solid #cccccc;
            border-radius: 4px;
            text-align: center;
            background-color: #f0f0f0;
        }
        QProgressBar::chunk {
            background-color: #2196F3;
            border-radius: 3px;
        }
    )";
    setProgressStyle(progressStyle);
    
    // 设置按钮样式
    QString buttonStyle = R"(
        QPushButton {
            background-color: #f0f0f0;
            border: 1px solid #cccccc;
            border-radius: 4px;
            padding: 5px 15px;
        }
        QPushButton:hover {
            background-color: #e0e0e0;
        }
        QPushButton:pressed {
            background-color: #d0d0d0;
        }
    )";
    m_cancelButton->setStyleSheet(buttonStyle);
} 